<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>广告修改</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">内容</label>
                <div class="layui-form-item">
                <div class="layui-input-block">
                    <textarea name="event" id="file" cols="30" rows="10"></textarea>
                  <!--  <div class="layui-btn-group" style="margin-top: 10px">
                        <button class="layui-btn" lay-event="getContent">介绍</button>
                    </div>-->
                </div>
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input id="starttime" name="starttime" placeholder="开始时间" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input id="finishtime" name="finishtime" placeholder="结束时间" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">地点</label>
                <div class="layui-input-inline">
                    <input type="text" name="addr" placeholder="地点" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">参加人数</label>
                <div class="layui-input-inline">
                    <input type="number" min="1" step="1" name="nums" placeholder="参加人数" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-inline">
                    <select id="RiverTypeLevel" name="stype" class="layui-input">
                        <option value="">类型</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">浏览人数</label>
                <div class="layui-input-inline">
                    <input type="text" name="visitnums" placeholder="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="" class="layui-input">
                </div>
            </div>
            <!--<div class="layui-form-item layui-inline">
                <label class="layui-form-label">图片</label>

                <div class="layui-input-block">
                    <input type="text"   name="imgurl" lay-verify="idcardpic" autocomplete="off" placeholder="照片" id="idcardpic"
                           class="layui-input">
                    <input class="btn-upload file-up" style="display: inline-block;" type="file" accept="image/*"  name="idcardpicUpload" id="idcardpicUpload" value="上传">
                </div>
                <div class="layui-input-block">
                    <img id="preview" height="200px" width="300px" src="#" alt="没有选择"/>
                    <span id="demoText"></span>
                </div>
            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">图片</label>
                <div class="layui-input-block">
                    <input type="text" name="coverimg" lay-verify="coverimg" autocomplete="off" placeholder=""
                           id="coverimg" class="layui-input">
                    <input class="btn-upload file-up" style="display: inline-block;" type="file"
                           accept="image/*" name="edit" id="img" value="上传">
                </div>
                <div class="layui-input-block">
                    <img id="preview" height="200px" width="300px" src="#" alt="没有选择" />
                    <span id="demoText"></span>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>


    var ids=[ "专题讲座","活动优惠","淘宝活动","专题精选","开业活动","Star专题","家长培训","公益讲座","精彩回顾","Star课程预售","公益活动","暑假班"];
    var names=[ "专题讲座","活动优惠","淘宝活动","专题精选","开业活动","Star专题","家长培训","公益讲座","精彩回顾","Star课程预售","公益活动","暑假班"];
    var select= document.getElementById('RiverTypeLevel');
    for (var i=0;i<ids.length;i++) {
        var option = document.createElement("option");
        option.setAttribute("value", ids[i]);
        option.innerText = names[i];
        select.appendChild(option)
    }


    layui.use('laydate', function() {
        var laydate = layui.laydate;

        // 初始化开始时间选择器
        laydate.render({
            elem: '#starttime',
            type: 'datetime',
            done: function(value, date) {
                // 在选择开始时间后，更新结束时间选择器的最小日期
                laydate.render({
                    elem: '#finishtime',
                    type: 'datetime',
                    min: value // 设置最小日期为选择的开始时间
                });
            }
        });

        // 初始化结束时间选择器
        laydate.render({
            elem: '#finishtime',
            type: 'datetime'
        });
    });
    layui.use(['form', 'jquery', 'laydate', 'tinymce','laytpl','upload'], function() {
        let form = layui.form;
        let $ = layui.jquery;
        var tinymce = layui.tinymce;
        var laytpl = layui.laytpl;
        var upload = layui.upload;
        let MODULE_PATH = "operate/";

        form.render("select");

        $.ajaxSetup({
            headers: {
                'Authorization': window.sessionStorage.getItem("token")
            }
        })

        var file = tinymce.render({
            elem: "#file",
            height: 400,
            automatic_uploads: true,
            images_upload_url: window.BASEPATH + "upload.do",
            images_upload_base_path: window.BASEPATH
        });

            $.getJSON(window.BASEPATH + "event/select/" + sessionStorage.getItem("id"),
                function (res) {
                    if (res.code != 0)
                        return layer.msg('信息检索失败');
                    let data = res.data;
                    file.setContent(data.event)
                    $("[name='event']").val(data.event);
                    $("[name='poster']").val(data.poster);
                    $("[name='starttime']").val(data.starttime);
                    $("[name='finishtime']").val(data.finishtime);
                    $("[name='addr']").val(data.addr);
                    $("[name='nums']").val(data.nums);
                    $("[name='stype']").val(data.stype);
                    $("[name='visitnums']").val(data.visitnums);
                    $("[name='title']").val(data.title);
                    $("[name='coverimg']").val(data.coverimg);
                    if (data.coverimg != null && data.coverimg.indexOf("http") == 0)
                        $("#preview").attr("src", data.coverimg);
                    else if (data.coverimg != null)
                        $("#preview").attr("src", window.BASEPATH + data.coverimg);

                    form.render();
                });




        form.on('submit(user-save)', function(data) {
            data.field.id=sessionStorage.getItem("id");
            data.field.event = file.getContent();
            $.ajax({
                url: window.BASEPATH+'event/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function(result) {
                    if (result.code==0) {
                        layer.msg(result.msg,{
                            icon: 1,
                            time: 1000
                        }, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })
            return false;
        });



        var uploadInst = upload.render({
            elem: '#img',
            url: window.BASEPATH + 'upload2',
            auto: true ,//选择文件后不自动上传
            choose: function(obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result) {
                    $('#preview').attr('src', result); //图片链接（base64）
                });
            },
            before: function(obj) {
                layer.msg('上传中', {
                    icon: 16,
                    time: 0
                });
            },
            done: function(res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                $("#coverimg").val(res.data);
                //上传成功的一些操作
                layer.msg("信息上传成功！", {
                    time: 3000
                });
            },
            error: function() {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html(
                    '<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>'
                );
                demoText.find('.demo-reload').on('click', function() {
                    uploadInst.upload();
                });
            }
        });
    })






</script>
<script>
</script>
</body>
</html>
